#include <netdb.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <stdio.h>

static void dump_hostent( hostent *hptr );
int main( int argc, char **argv )
{
	char *ptr;
	struct hostent *hptr;

	ptr = argv[1];

	/*
	   if((hptr = gethostbyname(ptr)) == NULL) {
		printf(" gethostbyname error for host:%s\n", ptr);
		return -1;
	   }
	 */

	char buf[2048];
	struct hostent hentry;
	int h_errno;
	int rc = gethostbyname_r( ptr, &hentry, buf, sizeof(buf), &hptr, &h_errno );
	if( rc != 0 ) {
		fprintf( stderr, "gethostname_r failed for hostname: %s, h_errno %d\n", ptr, h_errno );
		return -1;
	}

	dump_hostent( hptr );

	return 0;
}

static void dump_hostent( hostent *hptr )
{
	char **pptr;
	char str[32];

	printf( "official hostname:%s\n", hptr->h_name );
	for( pptr = hptr->h_aliases; *pptr != NULL; pptr++ )
		printf( " alias:%s\n", *pptr );

	switch( hptr->h_addrtype ) {
	case AF_INET:
	case AF_INET6:
		pptr = hptr->h_addr_list;
		for(; *pptr != NULL; pptr++ )
			printf( " address:%s\n",
					inet_ntop( hptr->h_addrtype, *pptr, str, sizeof(str)));
		printf( " first address: %s\n",
				inet_ntop( hptr->h_addrtype, hptr->h_addr, str, sizeof(str)));
		break;
	default:
		printf( "unknown address type\n" );
		break;
	}
}

